import {
  home,
} from '../../../../../api/index'

let listPagination = {
  index: 0,
  num: 10,
}
const {
  globalData: {
    IMGBASEURL
  }
} = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keywords: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMGBASEURL,
    list: [],
    listLoadStatus: 0,
    isRenderOver: true,
    activeIndex: 1,
    subtitle: 'new',
    tabs: [{
        text: '最新竞选',
        index: 1,
        val: 'new',
      },
      {
        text: '往期竞选',
        index: 2,
        val: 'previous'
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 选择二级分类
    clickTab(e) {
      this.setData({
        activeIndex: e.currentTarget.dataset['index'],
        subtitle: e.currentTarget.dataset.val
      })
      this.loadData(true)
    },
    onReachBottom() {
      if (this.data.listLoadStatus === 0 && this.data.isRenderOver) {
        this.loadData()
      }
    },
    async loadData(fresh = false) {
      this.setData({
        listLoadStatus: 1
      });
      const pageSize = listPagination.num;
      let pageIndex = listPagination.index + 1;
      let nextList = []
      if (fresh) {
        wx.pageScrollTo({
          scrollTop: 0,
        });
        pageIndex = 1;
        this.setData({
          list: []
        })
        this.setData({
          isShowHistory: false
        })
      }
      try {
        const res = await home.getCampaignList({
          "keyword": this.properties.keywords, //搜索关键字（必填）
          "subtitle": this.data.subtitle, //new 最新竞选 previous 往期竞选
          "PageIndex": pageIndex,
          "PageSize": pageSize
        })
        // console.log('竞选信息',res.Result.Datas);
        nextList = res.Result.Datas
        this.setData({
          list: fresh ? nextList : this.data.list?.concat(nextList),
        });
        this.setData({
          listLoadStatus: this.data.list?.length == res.Result.TotalCount ? 2 : 0,
        });
        wx.stopPullDownRefresh();
        listPagination.index = pageIndex;
        listPagination.num = pageSize;
      } catch (error) {
        console.log(error);
        this.setData({
          listLoadStatus: 3
        });
      }
    },
  }
})